<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	/*
 *      Counter.php
 *      
 *      Copyright 2011 godfather.typhoon.tycoon@banme <chungnguyenhuu911@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 *      
 *      
 */

	class  Counter   extends  CI_Controller{
		
		function  Counter  (){}
		function __construct(){
			parent::__construct();
			/*
			 * Load library
			 * 
			 */
			 $this->load->library('form_validation');
			$this->lang->load('messages','vietnamese'); 
			/* ---------------------------------------------------
			 * Check admin login
			 *  
			*/ 

			if(!$this->session->userdata('go_admin')){
				redirect('admin/login', 'refesh');
				return;
			}
		}
		function index(){
			$this->findAll();
		}
		function findAll(){
			/*
			 * 
			 * Conect from database
			 * Select all module from module table
			 * Display module view
			 * 
			 */ 
			$sql = "SELECT `id`,`name`, `name_en` FROM `counter` ";
			$query = $this->db->query($sql); 
			$data = array();
			if($query->num_rows() > 0){
				$data['counters'] = $query->result();
			}
			$data['main_content'] = 'manager/counter';
			$this->load->view('templates/manager/template', $data);
		}
		function findByID($id = -1){
			$sql = "SELECT `id`,`name`, `name_en`, `content` FROM `counter` WHERE `id` = ? limit 1";
			$query = $this->db->query($sql, array('id'=>$id)); 
			$data = array();
			if($query->num_rows() > 0){
				$data = $query->result();
			}
			return $data;
		}
		function update($name = "", $name_en = "", $content = "", $id = -1){
			$sql = "UPDATE `counter` SET `name` = ?, `name_en` = ?, `content` = ? ";
			$sql .= "WHERE `id` = ? ";
			echo $name_en;
			$query = $this->db->query($sql, array($name, $name_en, $content, $id));
			
		}
		function edit(){
			$data = array();
			/*
			 * 
			 * redirect from list module
			 * Get ID
			 */ 
			 
			$id = $this->uri->segment(3);
			
			/*
			 * Check id
			 * if GET id param is null or empty
			 * Get  POST id param
			 */ 
			if($id == null || $id ==""){
				$id = $this->input->post('id');
			}
			
			/*
			 * Get module by id
			 * return result set
			 */ 
			$result = $this->findByID($id);
			
			/*
			 * Set validate 
			 * 
			 */ 
			$this->form_validation-> set_rules('id', 'ID', 'is_natural_no_zero|xss_clean');
			$this->form_validation-> set_rules('name', 'Name', 'required|xss_clean');
			$this->form_validation-> set_rules('name_en', 'Name En', 'required|xss_clean');
			$this->form_validation-> set_rules('content', 'Content', 'is_natural|xss_clean');
			foreach($result as $row){
				
				//$data['module'] = $row;
				
				$data['id'] = $row->id;
				$data['name'] = $row->name;
				$data['name_en'] = $row->name_en;
				$data['content'] = $row->content;
			}
			if ($this->form_validation->run() == FALSE){
				$data['main_content'] = 'manager/editcounter';
				$this->load->view('templates/manager/template', $data);
				
			}else{
				$id = $this->input->post('id');
				$name = $this->input->post('name');
				$name_en = $this->input->post('name_en');
				$content = $this->input->post('content');
				$this->update($name, $name_en, $content, $id);
				$this->findAll();
			}
		}
	}
?>
